package com.huike.utils;

import com.jcraft.jsch.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class FileUploadUtil {
    private static final int DEFAULT_PORT = 22;
    private static final int DEF_WAIT_SECONDS = 30;

    private static String username = "root";
    private static String host = "101.132.69.205";
    private static String password = "520Liuwen";

    private static String dir = "/innovation_studio/images/cef";//文件存放基础目录

    /**
     * 创建服务器连接
     *
     *  waitSeconds
     *            等待秒数
     * @return
     */
    private static Session openSession() {
        Session session = null;
        try {
            JSch jsch = new JSch();
            session = jsch.getSession(username, host, DEFAULT_PORT);
            noCheckHostKey(session);
            session.setPassword(password);
            // 这个设置很重要，必须要设置等待时长为大于等于2分钟
            int waitSeconds = 120;
            session.connect(waitSeconds * 1000);
            if (!session.isConnected()) {
                throw new IOException("We can't connection to[" + host + "]");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return session;
    }

    /**
     * 文件上傳
     * @param fileName 上传后的文件名
     * @param inputStream 要上传的文件流
     * @return
     */
    public static String upload(String fileName,InputStream inputStream){
        String resUrl = null;

        Session session = openSession();
        ChannelShell openChannelShell = openChannelShell(session);
        openChannelShell.setInputStream(System.in);
        openChannelShell.setOutputStream(System.out);
        ChannelSftp openChannelSftp = openChannelSftp(session);
        try {
            openChannelSftp.cd(dir);
            openChannelSftp.put(inputStream,fileName);
            resUrl = "http://"+host+":8080/images/cef/"+fileName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        closeChannel(openChannelSftp);
        closeSession(session);
        return resUrl;
    }

    /**
     * 不作检查主机键值
     *
     * @param session
     */
    private static void noCheckHostKey(Session session) {
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
    }

    /**
     * 连接shell
     *
     * @param session
     *            session
     * @return {@link ChannelShell}
     */
    private static ChannelShell openChannelShell(Session session) {
        ChannelShell channel = null;
        try {
            channel = (ChannelShell) session.openChannel("shell");
            channel.setEnv("LANG", "en_US.UTF-8");
            channel.setAgentForwarding(false);
            channel.setPtySize(500, 500, 1000, 1000);
            channel.connect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (channel == null) {
            throw new IllegalArgumentException("The channle init was wrong");
        }
        return channel;
    }

    /**
     * 连接sftp
     *
     * @param session
     * @return {@link ChannelSftp}
     */
    private static ChannelSftp openChannelSftp(Session session) {
        ChannelSftp channel = null;
        try {
            channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return channel;
    }

    /**
     * 关闭Channel
     * @param channel
     */
    private static void closeChannel(Channel channel) {
        if (channel != null) {
            if (channel.isConnected()) {
                channel.disconnect();
            }
        }
    }

    /**
     * 关闭Session
     * @param session
     */
    private static void closeSession(Session session) {
        if (session != null) {
            if (session.isConnected()) {
                session.disconnect();
            }
        }
    }
}
